package com.guyu.common.core.exception;

import com.guyu.common.core.exception.code.ErrorCodeEnum;
import lombok.Getter;

import java.io.Serial;

/**
 * <p> 基础异常 </p>
 */
@Getter
public class BaseException extends RuntimeException {
    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 返回码
     */
    protected final Integer code;

    /**
     * 返回码描述
     */
    protected final String message;

    protected BaseException(Integer code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    protected BaseException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.message = message;
    }


    public static BaseException create(ErrorCodeEnum error) {
        return new BaseException(error.getCode(), error.getMessage());
    }

    public static BaseException create(ErrorCodeEnum error, String message) {
        return new BaseException(error.getCode(), message);
    }

    public static BaseException create(ErrorCodeEnum error, Throwable cause) {
        return new BaseException(error.getCode(), error.getMessage());
    }

    public static BaseException create(ErrorCodeEnum error, String message, Throwable cause) {
        return new BaseException(error.getCode(), error.getMessage());
    }
}
